import store from '@/store'
import { analysisComponentTypes } from './const'
import { ergodicList, getComponentListByTypes } from './utils'
import { getAllAnalysisComponentTypes } from './analysisData'

/**
 * 保存,返回当前的画布和页面配置信息
 * @returns
 */
export function save() {
  const { canvasComponentList, pageSetting } = store.state.lowCode
  const componentList = [...canvasComponentList]

  ergodicList(canvasComponentList, (component) => {
    component.animations.list.forEach((animation) => {
      animation.pending = false
    })
  })
  // 获取所有业务组件
  const types = getAllAnalysisComponentTypes()

  const itemList = getComponentListByTypes(componentList, types).map((item) => {
    const value = item.props.value
    // delete value.params
    return {
      ...value,
      categoryId: value.componentCategoryId,
      componentPageId: item.key,
    }
  })
  const pageOperation = JSON.stringify({ componentList, pageSetting })
  return {
    itemList,
    pageOperation,
  }
}
